package com.hnluchuan.bmjlearning.service;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.hnluchuan.bmjlearning.dao.CustomerServicePackageDAO;
import com.hnluchuan.bmjlearning.model.CustomerServicePackage;
import com.hnluchuan.bmjlearning.dto.CustomerServicePackageDTO;
import com.hnluchuan.core.support.Page;
import com.hnluchuan.utils.common.BeanMapper;
import org.springframework.transaction.annotation.Transactional;

@Service
public class CustomerServicePackageService extends BaseService {
	
	@Autowired
	private CustomerServicePackageDAO customerServicePackageDAO;
	
	public List<CustomerServicePackageDTO> find(CustomerServicePackageDTO query, Page page) {
		List<CustomerServicePackage> models = customerServicePackageDAO.find(query, page);
		List<CustomerServicePackageDTO> dtos = toDTOs(models);
		return dtos;
	}
	
	public List<CustomerServicePackageDTO> findAll() {
		return this.find(new CustomerServicePackageDTO(), null);
	}
	
	public List<CustomerServicePackageDTO> findAll(CustomerServicePackageDTO query) {
		return this.find(query, null);
	}
	
	public List<CustomerServicePackageDTO> find(Page page) {
		return this.find(new CustomerServicePackageDTO(), page);
	}
	
	public List<CustomerServicePackageDTO> find(int count) {
		Page page = new Page();
		page.setPage(1);
		page.setPageSize(count);
		return this.find(new CustomerServicePackageDTO(), page);
	}
	
	public Long create(CustomerServicePackageDTO dto) {
		CustomerServicePackage model = new CustomerServicePackage();
		toModel(model, dto);
		return customerServicePackageDAO.create(model);
	}

	public List<CustomerServicePackageDTO> findByAgentUserId(Long agentUserId){
		List<CustomerServicePackage> models = customerServicePackageDAO.findByAgentUserId(agentUserId);
		return toDTOs(models);
	}

	public CustomerServicePackageDTO load(Long id) {
	    CustomerServicePackage model = customerServicePackageDAO.load(id);
	    return toDTO(model);
    }

    public List<CustomerServicePackageDTO> findByCustomerId(Long customerId){
		List<CustomerServicePackage> models = customerServicePackageDAO.findByCustomerId(customerId);
		return toDTOs(models);
	}

	@Transactional(rollbackFor = Exception.class)
	public int deleteAllCustomerServicePackageByCustomerId(Long customerId){
    	return customerServicePackageDAO.deleteAllCustomerServicePackageByCustomerId(customerId);
	}

	public void updateAllFields(CustomerServicePackageDTO dto) {
		CustomerServicePackage model = customerServicePackageDAO.load(dto.getId());
		toModel(model, dto);
		customerServicePackageDAO.update(model);
    }

	public void deleteByIds(Long[] ids) {
		throw new RuntimeException("默认禁止删除，请确认是物理删除还是逻辑删除");
		/*
		if (ids != null) {
			for (Long id : ids) {
				customerServicePackageDAO.deleteById(id);
			}
		} */
    }
	
	public void deleteById(Long id) {
		this.deleteByIds(new Long[] {id});
	}

	public CustomerServicePackageDTO findOne(CustomerServicePackageDTO query) {
		CustomerServicePackage model = customerServicePackageDAO.findOne(query);
		return toDTO(model);
	}
	
	private List<CustomerServicePackageDTO> toDTOs(List<CustomerServicePackage> models) {
		if (CollectionUtils.isEmpty(models)) {
			return new ArrayList<CustomerServicePackageDTO>(0);
		}
		List<CustomerServicePackageDTO> dtos = new ArrayList<CustomerServicePackageDTO>(models.size());
		for (CustomerServicePackage model : models) {
	        CustomerServicePackageDTO dto = toDTO(model);
	        dtos.add(dto);
        }
	    return dtos;
    }
    
    private CustomerServicePackageDTO toDTO(CustomerServicePackage model) {
		if (model == null) {
			return null;
		}
		CustomerServicePackageDTO dto = BeanMapper.map(model, CustomerServicePackageDTO.class);
		
		return dto;
	}
	
	private void toModel(CustomerServicePackage model, CustomerServicePackageDTO dto) {
		BeanMapper.copy(dto, model);
    }
	
	@SuppressWarnings("unused")
	private List<CustomerServicePackage> toModels(List<CustomerServicePackageDTO> dtos) {
		if (CollectionUtils.isEmpty(dtos)) {
			return new ArrayList<CustomerServicePackage>(0);
		}
		List<CustomerServicePackage> models = new ArrayList<CustomerServicePackage>(dtos.size());
		for (CustomerServicePackageDTO dto : dtos) {
	        CustomerServicePackage model = new CustomerServicePackage();
	        toModel(model, dto);
	        models.add(model);
        }
		return models;
	}
}
